import {Component, ViewChild} from '@angular/core';
import { Storage } from '@ionic/storage';

import {
  Events,
  AlertController,
  App,
  ModalController,
  NavController,
  NavParams,
  ActionSheetController,
  Content,
  ViewController
} from 'ionic-angular';

import {Config} from '../../../providers/config';
import { Common } from '../../../providers/common';

import _ from 'lodash';
import {ContactChoosePage} from "../../contact/choose/choose.component";
import {FlowTypePage} from "../type/type.component";

@Component({
  selector: 'page-flow-query',
  templateUrl: './query.html'
})
export class FlowQueryPage{
	moa_page_name: any = "page-flow-query";
  moa_page_explain: any = "流程中心 - 高级查询";

  condition = null;
  flowTypes = [];

  defaultEmpByTree = [];

  //可以选择的最大日期
  maxDate = "";
  //结束日期可以选择的最小日期
  minEndDate = "1970-01-01";

  constructor(public alertCtrl: AlertController,
              public app: App,
              public modalCtrl: ModalController,
              public navCtrl: NavController,
              private navParams: NavParams,
              public events: Events,
              public config: Config,
              public actionSheetCtrl: ActionSheetController,
              public viewCtrl: ViewController,
              public storage: Storage,
              public common: Common) {
      //可以选择的日期不能超过今天
      this.maxDate = this.common.getNowFormatDate();
      // this.condition = this.defaultCondition;
      this.initCondition();
      this.storage.get("advancedQueryCondition")
        .then(data => {
          console.log("test" + data);
          if (data != null) {
            this.condition = data;
            this.storage.get("contact-user-list").then(userList => {
              let user = _.find(userList, {empid: this.condition.empid});
              if (user) {
                let delfaultUser = {
                  empid: user["empid"],
                  loginid: user["loginid"],
                  name: user["emname"],
                  orgid: user["orgid"],
                  titleid: user["titleid"],
                  type: "user"
                };
                this.defaultEmpByTree.push(delfaultUser);
              }
            });
          }
        });
  }

  cancel() {
    this.viewCtrl.dismiss("close");
  }

  /**
   * [chooseEmp 选择人员]
   * @param {[type]} item [description]
   */
  chooseEmp() {
    console.log("##authorization.detail.chooseEmpnames 选择外出人员");
    console.log("this.condition>>>>>>", this.condition);

    let that = this;
    let modal = this.modalCtrl.create(ContactChoosePage, {
      selected: this.defaultEmpByTree,
      isSelectUser: true,
      isSelectOne: true
    });

    modal.present();
    modal.onDidDismiss((data: any[]) => {
      console.info(data);
      if (data) {
        that.defaultEmpByTree = data;
        this.condition.empid = _.toString(_.map(data, "empid"));
        this.condition.empName = _.toString(_.map(data, "name"));
      }
    });
  }

  ionChange(){
      //设置结束日期的最小可选日期等于起始日期
      this.minEndDate = this.condition.startDate;
  }

  /**
   * 保存搜索条件并关闭Modal返回List页面加载搜索结果
   */
  search() {
      this.storage.set("advancedQueryCondition", this.condition);
      this.viewCtrl.dismiss("true");
  }

  /**
   * 清空查询条件
   */
  clearCondition() {
    this.initCondition();
    this.defaultEmpByTree = [];
    this.storage.remove("advancedQueryCondition");
  }

  /**
   * 打开流程类型选择页面
   */
  chooseFlowType() {
    let queryModal = this.modalCtrl.create(FlowTypePage);
    queryModal.onDidDismiss(data => {
      if (data && data != "close") {
        this.condition.flowTypeName = data.name;
        this.condition.bomid = data.bomid;
        this.condition.dirid = data.dirid;
      }
    });
    queryModal.present();
  }

  /**
   * 初始化查询条件对象
   */
  initCondition() {
    this.condition = {
      "title": "",
      "flowStatus": "",
      "empid":null,
      "empName":null,
      "startDate":null,
      "endDate":null,
      "bomid":null,
      "dirid":null,
      "flowTypeName":null
    };
  }

}

